package meijuli.com.join51.business.utils;

import android.content.Context;
import android.text.TextUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Random;
import java.util.TreeMap;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {

    public static boolean isEmpty(String msg) {
        return msg == null || msg.trim().length() == 0;
    }

    public static boolean isEmpty(CharSequence msg) {
        return msg == null || msg.toString().trim().length() == 0;
    }

    public static String hideCreditNumber(String creditNumber) {
        if (creditNumber != null && creditNumber.length() > 4) {
            String showPart = creditNumber.substring(creditNumber.length() - 4, creditNumber.length());
            return "**** **** **** " + showPart;
        }
        return "";
    }


    public static String hiePhoneNum(String phoneNum) {
        if (!TextUtils.isEmpty(phoneNum) && phoneNum.length() == 11) {
            return phoneNum.substring(0,3) + " **** " + phoneNum.substring(7, phoneNum.length());
        }
        return "";
    }

    public static String limitAccounNameLength(String account) {
        if (account != null && account.length() > 10) {
            account = account.substring(0, 10) + "...";
        }
        return account;
    }

    /**
     * 是否是有效的电话号码
     *
     * @param phoneNumber 电话号码
     */
    public static boolean isValidatePhoneNumber(String phoneNumber) {
        // 不验证手机号码
        if(TextUtils.isEmpty(phoneNumber)) {
            return false;
        }
        // 验证手机号
        return phoneNumber.startsWith("1");
    }

    /**
     * 判断是否为固定电话
     *
     * @param phoneNumber
     * @return
     */
    public static boolean isValidateFixedLineNumber(String phoneNumber) {
        String reg = "1([\\d]{10})|((\\+[0-9]{2,4})?\\(?[0-9]+\\)?-?)?[0-9]{7,8}";
        return Pattern.matches(reg, phoneNumber);
    }

    /**
     * 是否有效的昵称
     */
    public static boolean isValidateNickname(String nickname) {
        return nickname.matches("^[\u4e00-\u9fa5]+")
                && nickname.length() <= 12 && nickname.length() >= 3;
    }

    public static boolean isValidateAccount(String account) {
        String reg = "^[A-Za-z0-9]{6,16}+$";
        return Pattern.matches(reg, account);
    }

    /**
     * 获取唯一字符串
     */
    public static String getUniqueString() {
        return UUID.randomUUID().toString().replace("-", "") + Math.abs(new Random().nextInt(10000));
    }

    /**
     * 判断字符串是否有2-4个字符组成
     */
    public static boolean isChineseName(String chineseStr) {
        String rex = "[\\u4e00-\\u9fa5]{2,100}";
        return Pattern.matches(rex, chineseStr);
    }

    /**
     * 判断是否是有效的身份证号码
     */
    public static boolean isValidateIDNumber(String idNumber) {
        String rex = "((11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65)[0-9]{4})(([1|2][0-9]{3}[0|1][0-9][0-3][0-9][0-9]{3}[Xx0-9])|([0-9]{2}[0|1][0-9][0-3][0-9][0-9]{3}))";
        return Pattern.matches(rex, idNumber);
    }

    /**
     * 检测输入
     *
     * @param str 需要检测的字符串
     * @param regex 正则匹配字符串
     */
    public static Boolean checkInput(String str, String regex) {
        Pattern pat = Pattern.compile(regex);
        Matcher mat = pat.matcher(str);
        return mat.matches();
    }

    /**
     * 毫秒转换为日期
     *
     * @param milliseconds 毫秒
     * @return
     */
    public static String milliseconds2Date(long milliseconds) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
        String date = sdf.format(new Date(milliseconds));
        return date;
    }

    /**
     * 判断是否是有效的营业执照号码
     */
    public static boolean isValidateLicenceNum(String licenceNum) {
        String rex = "[\\\\d]{15}";
        return Pattern.matches(rex, licenceNum);
    }

    /**
     * 验证是否为有效的密码（长度为6-16个字符，不能包含空格，不能是9位以下纯数字）
     * 适用于登录时只非空验证
     *
     * @param password
     */
    public static final boolean isValidatePasswordByLogin(String password) {

        //        String regTemp = "^(?!\\d{1,8}$)[\\S]{6,16}$";

        //        return Pattern.matches(regTemp, password);

        return !StringUtil.isEmpty(password);
    }

    /**
     * 验证是否为有效的密码（长度为6-16个字符，不能包含空格，不能是9位以下纯数字）
     *
     * @param password
     */
    public static boolean isValidatePassword(String password) {
        String regTemp = "^(?!\\d{1,8}$)[\\S]{6,16}$";
        return Pattern.matches(regTemp, password);
    }

    /**
     * 是否为有效的验证码（不少于6位的数字）
     *
     * @param verifyCode
     * @return
     */
    public static boolean isValidateVerifyCode(String verifyCode) {
        String rex = "[0-9]{6,}";
        return Pattern.matches(rex, verifyCode);
    }

    /**
     * 认证的姓名是否有效(认证姓名不能包含特殊字符)
     *
     * @param authName 认证姓名
     * @return true有效的姓名 false无效的姓名
     */
    public static boolean containsSpecialCharacter(String authName) {
        if (StringUtil.isEmpty(authName)) {
            return true;
        }
        String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？\\d]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(authName);
        return m.find();
    }

    /**
     * 计算字符串长度
     *
     * @param authName 认证姓名
     * @return 认证姓名的长度
     */
    public static int calculateAuthNameLength(String authName) {
        if (StringUtil.isEmpty(authName)) {
            return 0;
        }
        int length = 0;
        char arr[] = authName.toCharArray();
        for (int i = 0; i < arr.length; i++) {
            char c = arr[i];
            if ((c >= 0x0391 && c <= 0xFFE5)) { //英文字符
                length = length + 2;
            } else if ((c >= 0x0000 && c <= 0x00FF)) { //中文字符
                length = length + 1;
            }
        }
        return length;
    }

    /**
     * 获取操作时间
     *
     * @param time
     * @return
     */
    public static String getOperaTime(long time) {
        return getOperaTime(System.currentTimeMillis(), time);
    }

    public static String getOperaTime(long serverTime, long operaTime) {
        if (operaTime != 0) {
            long duration = serverTime - operaTime;
            if (duration < 0) {
                return "1分前";
            }
            int hour = (int) (duration / (1000 * 60 * 60));
            int minute = (int) (duration % (1000 * 60 * 60) / (1000 * 60));
            if (hour == 0) {
                return minute + "分前";
            } else if (hour < 24) {
                return hour + "时前";
            } else if (hour > 24) {
                return hour / 24 + "天前";
            }
        }
        return "1分前";
    }

    /**
     * 米转换为KM
     */
    public static String m2KM(double m) {
        double km = m / 1000f;
        if (km < 1) {
            if (m == 0) {
                m = 1;
            }
            return (int) m + "m";
        } else {
            return (int) km + "km";
        }
        //        DecimalFormat decimalFormat = new DecimalFormat("##0.00");
        //        return decimalFormat.format(km) + "km";
    }

    /**
     * 使用字符串生成一个utf8字节数组
     *
     * @param pArray 字符串
     * @return utf8字节数组或null
     */
    public static byte[] getBytesUtf8(String pArray) {

        byte[] strByte = null;
        try {
            strByte = pArray.getBytes("UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strByte;
    }

    /**
     * 使用字节数组生成一个utf8字符串
     *
     * @param strByte 字节数组
     * @return utf8字符串或null
     */
    public static String newStringUtf8(byte[] strByte) {
        String result = null;
        try {
            result = new String(strByte, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 写入字符串
     *
     * @param content
     * @param file
     */
    public static void writeString(String content, File file) {
        Writer writer = null;
        try {
            writer = new FileWriter(file);
            writer.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * SHA256加密算法
     *
     * @param text
     * @return
     */
    public static String encodeToSHA256(String text) {
        return sha256(text);
    }

    public static String sha256(String plainText) {
        if (TextUtils.isEmpty(plainText)) {
            return "";
        }
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("SHA-256");
            byte[] hash = digest.digest(plainText.getBytes("UTF-8"));
            StringBuilder hexString = new StringBuilder();
            for (byte hex : hash) {
                String hexStr = Integer.toHexString(0xff & hex);
                if (hexStr.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hexStr);
            }
            return hexString.toString();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return plainText;
    }

    /**
     * 处理字符串，null转成空字符串
     */
    public static String getString(String value) {
        return isEmpty(value) ? "" : value;
    }

    /**
     * 过滤空字符串，空字符串显示默认字符串
     */
    public static String filterString(String value, String defaultValue) {
        return !isEmpty(value) ? value : defaultValue;
    }

    public static double getMultiplyString(double num,double fee) {
        BigDecimal bigDecimal = new BigDecimal(Double.toString(num));
        BigDecimal feeBigDecimal = new BigDecimal(Double.toString(fee));
        return bigDecimal.multiply(feeBigDecimal).doubleValue();
    }
    /**
     * 格式化字符,保留小数位 %[argument_index$][flags][width][.precision]conversion
     *
     * @param amount
     * @param fractionDigits
     * @return
     */
    public static String formatAmount(double amount, int fractionDigits) {
        return String.format(Locale.CHINA, "%." + fractionDigits + "f", amount);
    }

    public static String formatAmount(String amountStr, int fractionDigits) {
        if (!TextUtils.isEmpty(amountStr)) {
            double amount;
            try {
                amount = Double.parseDouble(amountStr);
                return formatAmount(amount, fractionDigits);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
            return amountStr;
        }
        return "";
    }

    public static boolean isValidServIP(String ip) {
        final String regex = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
                "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
                "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
                "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(ip);
        return matcher.matches();
    }

    public static boolean isValidServPort(int port) {
        return port > 0;
    }

    public static boolean isValidDomainName(String domainName) {
        final String regex = "^([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(domainName);
        return matcher.matches();
    }

    /**
     * 验证是否是胖胖生活回调协议
     *
     * @param text
     * @return
     */
    public static boolean isPPSHProtocol(String text, String key) {
        String regex = "^(?i)ppsh://.*" + key + ".*$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        return matcher.matches();
    }

    /**
     * 从字符串解析参数
     *
     * @param reqStr
     * @return
     */
    public static TreeMap<String, String> parseFromReqStr(String reqStr) {
        TreeMap<String, String> resMap = new TreeMap<>();
        if (null == reqStr || "".equals(reqStr)) {
            return resMap;
        }
        try {
            reqStr = URLDecoder.decode(reqStr, "utf-8");
        } catch (UnsupportedEncodingException e) {
            return resMap;
        }
        String[] reArray = reqStr.split("&");
        for (String tmp : reArray) {
            String[] tmpArr = tmp.split("=");
            if (tmpArr.length == 2) {
                resMap.put(tmpArr[0], tmpArr[1]);
            } else {
                resMap.put(tmpArr[0], "");
            }
        }
        return resMap;
    }

    public static String getNotEmptyStr(String startStr, String alternateStr) {
        if (TextUtils.isEmpty(alternateStr)) {
            return "null";
        }
        if (TextUtils.isEmpty(startStr)) {
            return alternateStr;
        }
        return startStr;
    }

    public static String getJson(Context context, String fileName) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    context.getAssets().open(fileName), "UTF-8"
            ));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return stringBuilder.toString();
    }

    public static double getDecimalDouble(double d, int decimalCnt) {
        double multipleNum = Math.pow(10, decimalCnt);
        return (double) Math.round(d * multipleNum) / multipleNum;
    }

    public static boolean isContainSpace(String s) {
        return s.contains(" ");
    }

}
